<!--
 * @Description: 
 * @Author: xunzhaotech
 * @Email: luyb@xunzhaotech.com
 * @QQ: 1525572900
 * @Date: 2025-03-21 17:42:03
 * @LastEditTime: 2025-03-22 19:08:29
 * @LastEditors: xunzhaotech
-->
<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="名称" prop="name">
        <el-input v-model="formData.name" placeholder="请输入名称" />
      </el-form-item>
      <el-form-item label="联系人" prop="contactPerson">
        <el-input v-model="formData.contactPerson" placeholder="请输入联系人" />
      </el-form-item>
      <el-form-item label="联系电话" prop="contactPhone">
        <el-input v-model="formData.contactPhone" placeholder="请输入联系电话" />
      </el-form-item>
      <el-form-item label="手机号码" prop="mobilePhone">
        <el-input v-model="formData.mobilePhone" placeholder="请输入手机号码" />
      </el-form-item>
      <el-form-item label="电子邮箱" prop="email">
        <el-input v-model="formData.email" placeholder="请输入电子邮箱" />
      </el-form-item>
      <el-form-item label="应收货款" prop="receivableMoney">
        <el-input-number
          v-model="formData.receivableMoney"
          placeholder="请输入应收货款"
          :precision="2"
          :step="0.01"
          :min="0"
        />
      </el-form-item>
      <el-form-item label="已收货款" prop="receivedMoney">
        <el-input-number
          v-model="formData.receivedMoney"
          placeholder="请输入已收货款"
          :precision="2"
          :step="0.01"
          :min="0"
        />
      </el-form-item>
      <el-form-item label="欠款" prop="debtMoney">
        <el-input-number
          v-model="formData.debtMoney"
          placeholder="请输入欠款"
          :precision="2"
          :step="0.01"
          :min="0"
        />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="formData.remark" type="textarea" placeholder="请输入备注" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { CustomerFollowUpPlanApi, CustomerFollowUpPlanVO } from '@/api/erp/customer/followupplan'

/** ERP 客户数据 表单 */
defineOptions({ name: 'CustomerDataForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  name: undefined,
  contactPerson: undefined,
  contactPhone: undefined,
  mobilePhone: undefined,
  email: undefined,
  receivableMoney: undefined,
  receivedMoney: undefined,
  debtMoney: undefined,
  remark: undefined
})
const formRules = reactive({
  name: [{ required: true, message: '名称不能为空', trigger: 'blur' }],
  contactPerson: [{ required: true, message: '联系人不能为空', trigger: 'blur' }],
  contactPhone: [{ required: true, message: '联系电话不能为空', trigger: 'blur' }],
  mobilePhone: [
    { required: false, message: '手机号码格式不正确', trigger: 'blur', pattern: /^1[3-9]\d{9}$/ }
  ],
  email: [{ required: false, message: '电子邮箱格式不正确', trigger: 'blur', type: 'email' }],
  receivableMoney: [{ required: true, message: '应收货款不能为空', trigger: 'blur' }],
  receivedMoney: [{ required: true, message: '已收货款不能为空', trigger: 'blur' }],
  debtMoney: [{ required: true, message: '欠款不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await CustomerFollowUpPlanApi.getCustomerFollowUpPlan(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as CustomerTagVO
    if (formType.value === 'create') {
      await CustomerTagApi.createCustomerTag(data)
      message.success(t('common.createSuccess'))
    } else {
      await CustomerTagApi.updateCustomerTag(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    name: undefined,
    remark: undefined,
    status: undefined,
    sort: undefined,
    customerIds: []
  }
  customerList.value = []
  formRef.value?.resetFields()
}
</script>
